package top.lightHearted.service;

import org.kurento.client.KurentoClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.lightHearted.bean.Room;

import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * RoomManager class
 *
 * @author gpf
 * @date 2018/04/16
 */
@Service
public class RoomManager {

    private final Logger logger = LoggerFactory.getLogger(RoomManager.class);

    @Autowired
    private KurentoClient client;

    private ConcurrentMap<String, Room> rooms = new ConcurrentHashMap<>();

    public Room getRoom(String name){
        Room room = rooms.get(name);
        if(room == null){
            room = new Room(client.createMediaPipeline(), name);
            rooms.put(name, room);
        }
        return room;
    }

    public void removeRoom(Room room) throws IOException {
        rooms.remove(room.getRoomName());
        room.close();
    }
}
